/**
 * @file walltester.cpp
 * @brief Wall automated tests implementation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3 of the License.
 *
 * @author VestniK (Sergey N.Vidyuk) sir.vestnik@gmail.com
 * @date 11 Oct 2008
 */
#include "wall.h"
#include "defaults.h"
#include "tileset.h"

#include <QtTest>
#include <QtCore>

class WallTester: public QObject {
   Q_OBJECT
   private slots:
      void initTestCase();
      void cleanupTestCase();
      void exceptionTests();
      void tileCountTest();
      void deadEndRefillingTest();
      void wallOverTest();
};

using namespace mjn;

void WallTester::initTestCase() {
}

void WallTester::cleanupTestCase() {
}

void WallTester::exceptionTests() {
   Wall wall;
   // Test 1
   try {
      wall.takeTile();
      QFAIL("Exception not thrown on attempt to get tile from empty wall");
   }
   catch ( WallEmptyException ) {
   }
   // Test 2
   try {
      wall.takeFromDeadEnd();
      QFAIL("Exception not thrown on attempt to get tile from empty dead end");
   }
   catch ( DeadEndEmptyException e ) {
   }
}

void WallTester::tileCountTest() {
   Wall wall;
   // 10 is just big enough number. The number of tile on the wall should not
   // change after wall rebuilding.
   TileSet set;
   set.fillSet();
   for ( int i = 0; i < 10; i++ ) {
      wall.buildWall(set);
      QCOMPARE( wall.getWallSize()+wall.getDeadEndSize() , (unsigned)TILE_COUNT );
   }
}

void WallTester::deadEndRefillingTest() {
   Wall wall;
   TileSet set;
   set.fillSet();
   wall.buildWall(set);
   // 10 is just big enough number. Dead end should never become empty until
   // the number of tiles on the wall greater then 14
   for (int i = 0; i < 10; i++) {
      wall.takeFromDeadEnd();
      QVERIFY( wall.getDeadEndSize() != 0 );
   }
}

void WallTester::wallOverTest() {
   Wall wall;
   TileSet set;
   set.fillSet();
   wall.buildWall(set);
   while ( wall.getWallSize() > MINIMUM_WALL_SIZE ) {
      QVERIFY( ! wall.isOver() );
      QVERIFY( wall.takeTile() != -1 );
   }
   QVERIFY( wall.isOver() );
   QVERIFY( wall.takeTile() == -1 );
}

QTEST_MAIN(WallTester);

#include "walltester.moc"
